package com.cxh.rmsq.ui.activity.self

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.CheckBox
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.cxh.rmsq.R
import com.cxh.rmsq.net.newNet.ProjectViewModel
import com.cxh.rmsq.ui.activity.LoginActivity
import com.cxh.rmsq.ui.activity.TitleBaseActivity
import com.cxh.rmsq.utils.ToastUtils
import com.cxh.rmsq.viewmodel.UserInfoViewModel
import com.hjq.shape.view.ShapeEditText
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.enums.PopupAnimation
import io.rong.callkit.util.SPUtils
import io.rong.imkit.feature.resend.ResendManager

class LogoffActivity : TitleBaseActivity() {

    private val tvSubmit: TextView by lazy { findViewById<TextView>(R.id.tv_submit) }
    private val checkbox1: CheckBox by lazy { findViewById<CheckBox>(R.id.checkbox1) }
    private val checkbox2: CheckBox by lazy { findViewById<CheckBox>(R.id.checkbox2) }
    private val checkbox3: CheckBox by lazy { findViewById<CheckBox>(R.id.checkbox3) }
    private val checkbox4: CheckBox by lazy { findViewById<CheckBox>(R.id.checkbox4) }
    private val checkbox5: CheckBox by lazy { findViewById<CheckBox>(R.id.checkbox5) }
    private val etCause: ShapeEditText by lazy { findViewById<ShapeEditText>(R.id.et_cause) }

    private var userInfoViewModel: UserInfoViewModel? = null
    private val viewModel by lazy {
        ViewModelProvider.NewInstanceFactory().create(ProjectViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_logoff)
//        ImmersionBar.with(this)
//            .fitsSystemWindows(true)
//            .statusBarColor(R.color.white) //状态栏颜色，不写默认透明色
//            .statusBarDarkFont(false) //状态栏字体是深色，不写默认为亮色
//            .navigationBarColor(R.color.transparent) //导航栏颜色，不写默认黑色
//            .navigationBarDarkIcon(false) //导航栏图标是深色，不写默认为亮色
//            .autoDarkModeEnable(true) //自动状态栏字体和导航栏图标变色，必须指定状态栏颜色和导航栏颜色才可以自动变色哦
//            .hideBar(BarHide.FLAG_SHOW_BAR) //隐藏状态栏或导航栏或两者，不写默认不隐藏
//            .fullScreen(true)
//            .init()
        titleBar.tvTitle.text = "注销账号"
        userInfoViewModel = ViewModelProviders.of(this).get(UserInfoViewModel::class.java)

        tvSubmit.setOnClickListener {
            backTips()
        }
    }

    var popupView: BasePopupView? = null
    private fun backTips() {
        popupView = XPopup.Builder(this)
            .hasNavigationBar(false)
            .isViewMode(true)
            .isDestroyOnDismiss(true)
            .hasBlurBg(true) //是否设置背景为高斯模糊背景。默认为false
            .dismissOnTouchOutside(false) //设置点击弹窗外面是否关闭弹窗，默认为true
            .autoDismiss(false) //设置当操作完毕后是否自动关闭弹窗，默认为true。比如：点击Confirm弹窗的确认按钮默认是关闭弹窗，如果为false，则不关闭
            .popupAnimation(PopupAnimation.NoAnimation) // 为弹窗设置内置的动画器，默认情况下，已经为每种弹窗设置了效果最佳的动画器；如果你不喜欢，仍然可以修改。
            //                .setPopupCallback(DemoXPopupListener()) // 设置弹窗显示和隐藏的回调监听
            //.asCustom(new LoginPopup(getContext())); // 登陆弹窗样式
            /*
                         * title:标题
                         * content：内容
                         * cancelBtnText：取消按钮文本(左边按钮文本)
                         * confirmBtnText：确定按钮文本(右边按钮文本)
                         * confirmListener：确定按钮点击事件(右边按钮点击事件)
                         * cancelListener：取消按钮点击事件(左边按钮点击事件)
                         * isHideCancel：是否隐藏左侧按钮(设置单个点击按钮使用)
                         */
            .asConfirm("温馨提示", "您确定要注销当前账号吗？",
                "确定", "取消",
                {
                    popupView?.dismiss()
                }, {
                    viewModel.getLogoff {
                        if (it.code == 200) {
                            ToastUtils.showToast("注销成功")
                            finishLogout(true)
                        } else {
                            ToastUtils.showToast(it.msg)
                        }
                    }
                    popupView?.dismiss()

                }, false/*, R.layout.custom_confirm_pop*/
            ).show()
    }

    private fun finishLogout(isAccountDeleted: Boolean) {
        ResendManager.getInstance().removeAllResendMessage()
        logout(isAccountDeleted)
        // 通知退出
        sendLogoutNotify()
        SPUtils.clear(applicationContext)
        com.blankj.utilcode.util.SPUtils.getInstance().clear()
        startActivity(Intent(this, LoginActivity::class.java))
        finish()
    }

    private fun logout(isAccountDeleted: Boolean) {
        if (userInfoViewModel != null) {
            if (isAccountDeleted) {
                userInfoViewModel!!.accountDelete()
            } else {
                userInfoViewModel!!.logout()
            }
        }
    }

    fun onCheckboxClicked(view: View) {
        // 这是View.OnClickListener的一个简便写法，它会传递触发事件的视图对象。
        val checked = (view as CheckBox).isChecked
        when (view.getId()) {
            R.id.checkbox1 -> {
                isCheck()
                checkbox1.isChecked = true
            }

            R.id.checkbox2 -> {
                isCheck()
                checkbox2.isChecked = true
            }

            R.id.checkbox3 -> {
                isCheck()
                checkbox3.isChecked = true
            }

            R.id.checkbox4 -> {
                isCheck()
                checkbox4.isChecked = true
            }

            R.id.checkbox5 -> {
                isCheck()
                checkbox5.isChecked = true
                etCause.visibility = View.VISIBLE
            }
        }
    }

    private fun isCheck() {
        checkbox1.isChecked = false
        checkbox2.isChecked = false
        checkbox3.isChecked = false
        checkbox4.isChecked = false
        checkbox5.isChecked = false
        etCause.visibility = View.GONE
    }
}